package edu.must.uh.util;

import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import org.apache.log4j.Logger;

public class SqlMsDataSource implements DataSource{
    
    private static SqlMsDataSource instance = new SqlMsDataSource();;
    private SqlMsDataSource() {
        
    }
    public static SqlMsDataSource getInstance() {
       
        return instance;
    }    
   
    private static  String DriverName = " ";
    private static  String url = " ";
    private static  String username = " ";
    private static  String password = " ";
    private static Logger logger = Logger.getLogger(SqlMsDataSource.class);
    private static Connection conn =null;
    static{
        try {
            ResourceBundle res = ResourceBundle.getBundle("uh");
            url = res.getString("msssql.url"); 
            username = res.getString("msssql.username");  
            password = res.getString("msssql.password"); 
            DriverName = res.getString("msssql.driver"); 
            Class.forName(DriverName);      
            conn = DriverManager.getConnection(url, username, password);
            logger.info("-----------------------mssql  connect successfully--------------------------");
        } catch (Exception e) {
            logger.error("mssql connect fail",e); 
            throw new RuntimeException("mssql connect fail");
        }
    }
    
    @Override
    public Connection getConnection() throws SQLException {
        
        if(conn == null || conn.isClosed()) {            
            conn = DriverManager.getConnection(url, username, password);
        }
        return conn;
    }
    

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        // TODO Auto-generated method stub
        return false;
    }


    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

  

}
